package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.CouponMyListVo;
import com.cskaoyan.bean.vo.WxCouponSelectListVo;
import com.cskaoyan.service.WxCouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author gatsby
 * @since 2022/10/14 20:04
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;


    /**
     * 查询当前用户的全部优惠券的业务逻辑
     *
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/14 20:05
     */
    @RequestMapping("mylist")
    public BaseRespVo myList(BasePageInfo basePageInfo, Short status) {
        CommonData<CouponMyListVo> commonData = wxCouponService.queryCouponLimit(basePageInfo, status);
        return BaseRespVo.ok(commonData, "成功");
    }

    /**
     * 查询全部优惠券的业务逻辑
     *
     * @param basePageInfo
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/14 23:35
     */
    @GetMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo) {
        CommonData<MarketCoupon> commonData = wxCouponService.queryAllCouponLimit(basePageInfo);
        return BaseRespVo.ok(commonData, "成功");
    }

    /**
     * 领取优惠券的业务逻辑
     *
     * @param
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/14 23:35
     */
    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody Map map) {
        Integer couponId = (Integer) map.get("couponId");
        Subject subject = SecurityUtils.getSubject();
        //判断用户是否登录
        int code=0;
        if(subject.isAuthenticated()){
            code = wxCouponService.receiveCoupon(couponId);
        }else{
            return new BaseRespVo(null,"请登录",501);
        }
        if (code == 740) {
            return new BaseRespVo(null, "该优惠券已领取完毕", code);
        }
        return BaseRespVo.ok("成功");
    }

    /**
     * 兑换优惠券的业务逻辑
     * @param map
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/15 9:05
     */
    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map) {
        String code = (String) map.get("code");
        int errorno=wxCouponService.exchange(code);
        if(errorno==742){
            return new BaseRespVo(null,"优惠券不正确",errorno);
        }
        if(errorno==740){
            return new BaseRespVo(null,"优惠券已兑换",errorno);
        }
        return BaseRespVo.ok("成功");
    }

    @RequestMapping("selectlist")
    public BaseRespVo selectList(Integer cartId,Integer grouponRulesId){
        CommonData<CouponMyListVo> commonData=wxCouponService.selectList(cartId,grouponRulesId);
        return BaseRespVo.ok(commonData,"成功");
    }

}
